package com.shen.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shen.eduservice.entity.EduChapter;
import com.shen.eduservice.entity.EduVideo;
import com.shen.eduservice.entity.chapter.ChapterVo;
import com.shen.eduservice.entity.chapter.VideoVo;
import com.shen.eduservice.mapper.EduChapterMapper;
import com.shen.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shen.eduservice.service.EduVideoService;
import com.shen.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author shenkaituo
 * @since 2020-06-21
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //根据课程id查询 课程里面的所有章节
        QueryWrapper<EduChapter> wrapperChapter=new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);//数据库里面的名字
        List<EduChapter> eduChapterList= baseMapper.selectList(wrapperChapter);

        //根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> wrapperVideo=new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);//数据库里面的名字
        List<EduVideo> eduVideoList = eduVideoService.list(wrapperVideo);
        List<ChapterVo> chapterFinalList=new ArrayList<>();
        //遍历查询章节list进行封装
        for(int i=0;i<eduChapterList.size();i++){
            EduChapter eduChapter=eduChapterList.get(i);
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);

//            chapterFinalList.add(chapterVo);
            //封装小节
            List<VideoVo> videoList =new ArrayList<>();

            //遍历查询小节list 进行封装

            for(int j=0;j<eduVideoList.size();j++){
                EduVideo eduVideo = eduVideoList.get(j);
                //判断chapterid和video里面的相等
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //放到
                    videoList.add(videoVo);

                }

                chapterVo.setChildren(videoList);

            }
            chapterFinalList.add(chapterVo);

        }


        return chapterFinalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
    //根据章节id查询小节
        QueryWrapper<EduVideo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(queryWrapper);
        if(count>0){
            //不删除
            throw new GuliException(20001,"不能删除");
        }
        else{
            int result = baseMapper.deleteById(chapterId);
            return result>0;

        }

    }

    @Override
    public void removeByCourseId(String id) {
        QueryWrapper<EduChapter> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id",id);
        baseMapper.delete(queryWrapper);
    }
}
